<?php
namespace  app\admin\controller;
use app\admin\model\Admin_model;
use think\Controller;
use think\Db;
use think\Request;

Class Manageuser extends Controller{
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        if(session('user')) {
            $user=session('user');
            $admin_model=new Admin_model();
            $fun=$admin_model->get_rolefunction($user['role']);
            $this->assign('fun',$fun);
            $this->assign('active','个人中心');

        }else{
            $this->redirect('index/login/index');
        }
    }

    //user_info
    function user_info(){
        $user=session('user');
        switch ($user['role']){
            case DLS:
                $this->redirect('agent_user_info');
                break;
            case ZZH:
                $this->redirect('child_user_info');
                break;
            case GGZ:
                $this->redirect('adv_user_info');
                break;
            default:
                $this->redirect('index/login/index?&msg=账号有误，重新登陆');
        }
    }

    //代理商信息
    function  agent_user_info(){
        $user=session('user');
        if(request()->isPost()){
            $data = input('post.');
            unset($data['file']);
            $info=Db::name('agent_user')->update($data);
            if($info){
                return array('code' => 1, 'msg' => '更改成功', 'url' => url('agent_user_info'));
            }else{
                return array('code' => 0, 'msg' => '未进行修改',);
            }
        }
        $agent= Db::name('agent_user')->where('name',$user['username'])->find();
        if (empty($agent)){
            $agent['name']=$user['username'];
            $id=Db::name('agent_user')->insertGetId($agent);
            $agent['id']=$id;
            $agent= Db::name('agent_user')->where('name',$user['username'])->find();
        }
        $this->assign('agent',$agent);
        $this->assign('title','账户信息');
        return $this->fetch();
    }
    //子账户信息
    function child_user_info(){
        $user=session('user');
        if(request()->isPost()){
            $data = input('post.');

            $info=Db::name('child_user')->update($data);
            if($info){
                return array('code' => 1, 'msg' => '更改成功', 'url' => url('child_user_info'));
            }else{
                return array('code' => 0, 'msg' => '未进行修改',);
            }
        }
        $child_user= Db::name('child_user')->where('name',$user['username'])->find();
        $this->assign('agent',$child_user);
        $this->assign('title','账户信息');
        return $this->fetch();
    }
    //广告主信息
    function  adv_user_info(){
        $user=session('user');
        if(request()->isPost()){
            $data = input('post.');
            unset($data['file']);
            $info=Db::name('advertisers')->update($data);
            if($info){
                return array('code' => 1, 'msg' => '更改成功', 'url' => url('adv_user_info'));
            }else{
                return array('code' => 0, 'msg' => '未进行修改',);
            }
        }
        $child_user= Db::name('advertisers')->where('name',$user['username'])->find();
        $this->assign('agent',$child_user);
        $this->assign('title','账户信息');
        return $this->fetch();
    }
    //账户安全
    function  user_safe(){
        $this->assign('title','账户安全');
        return $this->fetch();
    }
    function  edit_pwd(){
        $user=session('user');
        $data=input('post.');
        $u= Db::name('user')->where('id',$user['id'])->find();
        if($u['password']==$data['old_pwd']){
            $info=Db::name('user')->where('id',$user['id'])->update(array('password'=>$data['new_pwd']));
            if($info){
                session('user',null);
                return array('code'=>1,'msg'=>'更改密码成功，重新登录','url'=>url('index/login/index'));
            }else{
                return array('code'=>0,'msg'=>'更改密码失败');
            }
        }else{
            return array('code'=>0,'msg'=>'原密码输入错误');
        }
    }
    //重置密码
    function reset_pwd(){
        $user_id=$_GET['user_id'];
        $new_pwd=rand('100000','999999');
        $info= Db::name('user')->where('id',$user_id)->update(array('password'=>$new_pwd));
        if($info){
            return array('code'=>1,'msg'=>'重置密码成功，新密码为：'.$new_pwd);
        }else{
            return array('code'=>0,'msg'=>'重置失败');
        }

    }

}